﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;
using STX.Domain;
using System.ComponentModel.DataAnnotations.Schema;

namespace STX.Repository
{
    public class Mapping
    {
        public void RegisterTo(DbModelBuilder modelBuilder)
        {
            //RegisterTopic(modelBuilder);
            RegisterTabAdministrator(modelBuilder);
            RegisterTABMenu(modelBuilder);
            RegisterTABRoleGroup(modelBuilder);
            RegisterTABRole(modelBuilder);
            RegisterTABUserGroup(modelBuilder);
            RegisterTabBlackList(modelBuilder);
            RegisterTabCategory(modelBuilder);
            RegisterActivityItemSupplier(modelBuilder);
            #region 业务逻辑
            RegisterBizUser(modelBuilder);

            RegisterBizArea(modelBuilder);
            RegisterBizMaterialCategory(modelBuilder);
            RegisterBizSupplier(modelBuilder);
            RegisterBizQuotePriceTemplate(modelBuilder);
            RegisterBizQuotePriceList(modelBuilder);
            RegisterBizActivity(modelBuilder);
            RegisterBizActivityItem(modelBuilder);
            RegisterBizProject(modelBuilder);
            RegisterBizNotice(modelBuilder);
            RegisterBizMessage(modelBuilder);
            RegisterBizDownload(modelBuilder);
            RegisterBizApply(modelBuilder);
            RegisterBizPayInfo(modelBuilder);

            #endregion

        }

        #region 后台菜单
        /// <summary>
        /// 后台菜单
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void RegisterTABMenu(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<TabMenu>();
            config.ToTable("Menu", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();

            config.Property(t => t.Link)
                .HasColumnName("Link")
                .HasMaxLength(200);

            config.Property(t => t.TitleImg)
                .HasColumnName("TitleImg")
                .HasMaxLength(200);

        }
        #endregion

        #region 角色组
        /// <summary>
        /// 角色组
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void RegisterTABRoleGroup(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<TabPermissionGroup>();
            config.ToTable("PermissionGroup", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();

            config.Property(t => t.Info)
                .HasColumnName("Info")
                .HasMaxLength(200)
                .IsOptional();
        }
        #endregion

        #region 角色
        /// <summary>
        /// 角色
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void RegisterTABRole(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<TabPermission>();
            config.ToTable("Permission", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();

            config.Property(t => t.Info)
                .HasColumnName("Info")
                .HasMaxLength(200)
                .IsOptional();

            config.HasRequired(t => t.TabPermissionGroup).WithMany().HasForeignKey(t => t.GroupId);

        }
        #endregion

        #region 用户组
        /// <summary>
        /// 角色组
        /// </summary>
        /// <param name="modelBuilder"></param>
        private void RegisterTABUserGroup(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<TabUserGroup>();
            config.ToTable("UserGroup", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .HasColumnName("Name")
                .HasMaxLength(50)
                .IsRequired();

            config.Property(t => t.Info)
                .HasColumnName("Info")
                .HasMaxLength(200)
                .IsOptional();
        }
        #endregion

        #region 黑名单
        private void RegisterTabBlackList(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<TabBlackList>();
            config.ToTable("BlackList", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.IpAddress)
                .IsRequired()
                .HasMaxLength(100);
        } 

        #endregion

        #region 类别管理
        private void RegisterTabCategory(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<TabCategory>();
            config.ToTable("Category", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(120);

            config.Property(t => t.DisplayName)
                .IsOptional()
                .HasMaxLength(32);

            config.Property(t => t.ImgPath)
                .HasMaxLength(200)
                .IsOptional();

            config.Property(t => t.Info)
                  .IsOptional();

            config.Property(t => t.Template)
                  .HasMaxLength(100)
                  .IsOptional();

            config.Property(t => t.SeoKeywords)
                  .HasMaxLength(200)
                  .IsOptional();

            config.Property(t => t.SeoDescription)
                  .HasMaxLength(400)
                  .IsOptional();          
        }
        #endregion


        private void RegisterTopic(DbModelBuilder modelBuilder)
        {
            ////Store
            //var config = modelBuilder.Entity<TabTopic>();
            //config.ToTable("Topic", "Biz");
            //config.HasKey(t => t.Id);

            //config.Property(t => t.Id)
            //    .HasColumnName("Id")
            //    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            //    .IsRequired();

            //config.Property(t => t.Title)
            //    .HasColumnName("Title")
            //    .HasMaxLength(200)
            //    .IsRequired();
        }



        #region 业务逻辑部分

        #region 项目
        private void RegisterBizProject(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizProject>();
            config.ToTable("Project", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(100);

            config.HasRequired(t => t.BizArea)
                .WithMany()
                .HasForeignKey(t => t.AreaId).WillCascadeOnDelete(false);
        }

        #endregion

        #region 区域
        private void RegisterBizArea(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizArea>();
            config.ToTable("Area", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(40);
        }

        #endregion

        #region 物料分类表
        private void RegisterBizMaterialCategory(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizMaterialCategory>();
            config.ToTable("MaterialCategory", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(100);
        }
        #endregion

        #region 供应商管理表
        private void RegisterBizSupplier(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizSupplier>();
            config.ToTable("Supplier", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(200);

            config.HasRequired(t => t.BizArea)
                .WithMany()
                .HasForeignKey(t => t.AreaId);

        }
        #endregion

        #region 报价模板
        private void RegisterBizQuotePriceTemplate(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizQuotePriceTemplate>();
            config.ToTable("QuotePriceTemplate", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Name)
                .IsRequired()
                .HasMaxLength(120);

            config.Property(t => t.Code)
                .IsRequired()
                .HasMaxLength(120);

            config.Property(t => t.Unit)
                .IsRequired()
                .HasMaxLength(10);


            config.HasRequired(t => t.BizMaterialCategory)
                .WithMany()
                .HasForeignKey(t => t.MaterialCategoryId);

        }
        #endregion

        #region 报价模板价格表
        private void RegisterBizQuotePriceList(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizQuotePrice>();
            config.ToTable("QuotePrice", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();


            config.HasRequired(t => t.BizQuotePriceTemplate)
                .WithMany(o=>o.BizQuotePriceList)
                .HasForeignKey(t => t.TemplateId);
            
        }
        #endregion

        #region 活动
        private void RegisterBizActivity(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizActivity>();
            config.ToTable("Activity", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.HasRequired(t => t.BizProject)
                .WithMany()
                .HasForeignKey(t => t.ProjectId);

            config.HasRequired(t => t.Administrator)
                .WithMany()
                .HasForeignKey(t => t.AdministratorId);
        }
        #endregion

        #region 活动
        private void RegisterBizActivityItem(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizActivityItem>();
            config.ToTable("ActivityItem", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.HasRequired(t => t.BizMaterialCategory)
                .WithMany()
                .HasForeignKey(t => t.MaterialCategoryId);

            config.Property(t => t.TpCode)
                .IsOptional()
                .HasMaxLength(120);

            config.HasOptional(t => t.BizSupplier)
                .WithMany()
                .HasForeignKey(t => t.SupplierId);


            config.HasRequired(t => t.BizActivity)
                .WithMany(o=>o.BizActivityItems)
                .HasForeignKey(t => t.ActivityId);

        }
        #endregion

        #region 物料供应商
        private void RegisterActivityItemSupplier(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizActivityItemSupplier>();
            config.ToTable("ActivityItemSupplier", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.HasRequired(t => t.BizSupplier)
                .WithMany()
                .HasForeignKey(t => t.SupplierId);


            config.HasRequired(t => t.BizActivityItem)
                .WithMany(o => o.BizActivityItemSuppliers)
                .HasForeignKey(t => t.ActivityItemId);

        }
        #endregion

        #region 用户 User
        private void RegisterBizUser(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<BizUser>();
            config.ToTable("User", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Username)
                .IsRequired()
                .HasMaxLength(20);

            config.Property(t => t.Password)
                .IsRequired()
                .HasMaxLength(32);

            config.Property(t => t.Email)
                .IsRequired()
                .HasMaxLength(200);

            config.Property(t => t.Photo)
                .IsOptional()
                .HasMaxLength(200);

            config.Property(t => t.LastLoginIp)
                .IsOptional()
                .HasMaxLength(15);

        }
        #endregion

        private void RegisterBizNotice(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<BizNotice>();
            config.ToTable("Notice", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Title)
                .IsRequired()
                .HasMaxLength(200);

        }

        private void RegisterBizMessage(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<BizMessage>();
            config.ToTable("Message", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Title)
                .IsRequired()
                .HasMaxLength(200);
        }

        private void RegisterBizDownload(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<BizDownload>();
            config.ToTable("Download", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Title)
                .IsRequired()
                .HasMaxLength(200);

            config.Property(t => t.FileUrl)
                .IsOptional()
                .HasMaxLength(200);
        }

        private void RegisterBizApply(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<BizApply>();
            config.ToTable("Apply", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.Title)
                .IsRequired()
                .HasMaxLength(200);

            config.HasRequired(t => t.Administrator)
                .WithMany()
                .HasForeignKey(t => t.UserId);
        }


        private void RegisterBizPayInfo(DbModelBuilder modelBuilder)
        {
            var config = modelBuilder.Entity<BizPayInfo>();
            config.ToTable("PayInfo", "Biz");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            

            config.HasRequired(t => t.Activity)
                .WithMany()
                .HasForeignKey(t => t.ActivityId);

            config.HasRequired(t => t.Supplier)
                .WithMany()
                .HasForeignKey(t => t.SupplierId);
        }

        //private void RegisterBUSAccount(DbModelBuilder modelBuilder)
        //{
        //    //Store
        //    var config = modelBuilder.Entity<BUS_Account>();
        //    config.ToTable("Account", "BUS");
        //    config.HasKey(t => t.Id);

        //    config.Property(t => t.Id)
        //        .HasColumnName("Id")
        //        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
        //        .IsRequired();

        //    config.Property(t => t.UserName)
        //        .IsRequired()
        //        .HasMaxLength(50);

        //    config.Property(t => t.NickName)
        //        .IsRequired()
        //        .HasMaxLength(20);

        //    config.Property(t => t.Password)
        //        .IsRequired()
        //        .HasMaxLength(32);

        //    config.Property(t => t.Photo)
        //        .HasMaxLength(200)
        //        .IsOptional();

        //    config.Property(t => t.Hobby)
        //          .HasMaxLength(500)
        //          .IsOptional();


        //    //config.Property(t => t.Title)
        //    //    .HasColumnName("Title")
        //    //    .HasMaxLength(200)
        //    //    .IsRequired();
        //}

        #endregion


        private void RegisterTabAdministrator(DbModelBuilder modelBuilder)
        {
            //Store
            var config = modelBuilder.Entity<TabAdministrator>();
            config.ToTable("Administrator", "Tab");
            config.HasKey(t => t.Id);

            config.Property(t => t.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            config.Property(t => t.UserName)
                .IsRequired()
                .HasMaxLength(20);

            config.Property(t => t.Password)
                .IsRequired()
                .HasMaxLength(32);

            config.Property(t => t.Address)
                .HasMaxLength(200)
                .IsOptional();

            config.Property(t => t.EMail)
                  .HasMaxLength(200)
                  .IsOptional();

            config.Property(t => t.Token)
                  .HasMaxLength(32)
                  .IsOptional();

            config.Property(t => t.Photo)
                  .HasMaxLength(200)
                  .IsOptional();

            //config.Property(t => t.Title)
            //    .HasColumnName("Title")
            //    .HasMaxLength(200)
            //    .IsRequired();
        }

    }


}
